package com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import com.huawei.appgallery.foundation.ui.framework.activity.AbstractBaseActivity;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.secure.android.common.intent.SafeIntent;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import o.ayw;
import o.ayy;
import o.bkv;
import o.ckh.e;
import o.ckj;
import o.cks;
import o.cku;
import o.cku.c;
import o.Logger;
import o.evd;

public class DialogActivity extends Activity {
    private cku ˊ = null;
    private long ˋ;
    private a ˎ;
    private d ॱ;

    public static class a {
        private CharSequence ʻ;
        private int ʻॱ;
        private int ʼ;
        private Context ʼॱ;
        private o.ckh.a ʽ;
        private WeakReference<DialogFragment> ʾ;
        private String ˈ;
        private View ˊ;
        private ckj ˊॱ;
        private CharSequence ˋ;
        private OnKeyListener ˋॱ;
        private CharSequence ˎ;
        private CharSequence ˏ;
        private e ˏॱ;
        private OnDismissListener ͺ;
        private int ॱ;
        private OnCancelListener ॱˊ;
        private int ॱˋ = -1;
        private int ॱˎ = -2;
        private CharSequence ॱॱ;
        private boolean ॱᐝ = true;
        private int ᐝ;
        private Map<Integer, c> ᐝॱ = new HashMap();

        public a(@NonNull Context context, @NonNull String str) {
            this.ʼॱ = context;
            this.ˈ = str;
        }

        public a ˊ(@StringRes int i) {
            this.ˏ = this.ʼॱ.getString(i);
            return this;
        }

        public a ˏ(CharSequence charSequence) {
            this.ˏ = charSequence;
            return this;
        }

        public a ॱ(@StringRes int i) {
            this.ˎ = this.ʼॱ.getString(i);
            return this;
        }

        public a ॱ(CharSequence charSequence) {
            this.ˎ = charSequence;
            return this;
        }

        public a ˎ(View view, ckj o_ckj) {
            this.ˊ = view;
            this.ˊॱ = o_ckj;
            return this;
        }

        public a ˏ(o.ckh.a aVar) {
            this.ʽ = aVar;
            return this;
        }

        public a ॱ(e eVar) {
            this.ˏॱ = eVar;
            return this;
        }

        public a ˏ(OnCancelListener onCancelListener) {
            this.ॱˊ = onCancelListener;
            return this;
        }

        public a ˋ(OnDismissListener onDismissListener) {
            this.ͺ = onDismissListener;
            return this;
        }

        public a ˏ(OnKeyListener onKeyListener) {
            this.ˋॱ = onKeyListener;
            return this;
        }

        public a ˋ(int i, @StringRes int i2) {
            ˋ(i, this.ʼॱ.getString(i2));
            return this;
        }

        public a ˋ(int i, CharSequence charSequence) {
            if (i == -1) {
                this.ˋ = charSequence;
            } else if (i == -2) {
                this.ʻ = charSequence;
            } else if (i == -3) {
                this.ॱॱ = charSequence;
            }
            return this;
        }

        public a ˊ(int i, int i2) {
            if (i == -1) {
                this.ॱ = i2;
            } else if (i == -2) {
                this.ʼ = i2;
            } else if (i == -3) {
                this.ᐝ = i2;
            }
            return this;
        }

        public a ॱ(boolean z) {
            this.ॱᐝ = z;
            return this;
        }

        public View ˏ() {
            return this.ˊ;
        }

        private void ˏ(DialogFragment dialogFragment) {
            this.ʾ = new WeakReference(dialogFragment);
        }

        private void ʻ() {
            this.ʾ.clear();
        }

        public void ˎ() {
            long ॱ = bkv.ˎ().ॱ((Object) this);
            Intent intent = new Intent(this.ʼॱ, DialogActivity.class);
            intent.putExtra("dialog_activity_task_id", ॱ);
            intent.addFlags(this.ʻॱ);
            intent.addFlags(WXMediaMessage.THUMB_LENGTH_LIMIT);
            if (this.ʼॱ instanceof Activity) {
                this.ʼॱ.startActivity(intent);
                return;
            }
            Activity ʻॱ = AbstractBaseActivity.ʻॱ();
            if (ʻॱ != null) {
                ʻॱ.startActivity(intent);
                return;
            }
            intent.addFlags(268435456);
            this.ʼॱ.startActivity(intent);
        }

        public PendingIntent ॱ() {
            long ॱ = bkv.ˎ().ॱ((Object) this);
            Intent intent = new Intent(this.ʼॱ, DialogActivity.class);
            intent.putExtra("dialog_activity_task_id", ॱ);
            intent.addFlags(this.ʻॱ);
            intent.addFlags(WXMediaMessage.THUMB_LENGTH_LIMIT);
            return PendingIntent.getActivity(this.ʼॱ, 0, intent, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR);
        }

        public boolean ˋ() {
            if (this.ʾ != null) {
                DialogFragment dialogFragment = (DialogFragment) this.ʾ.get();
                if (!(dialogFragment == null || dialogFragment.getDialog() == null || !dialogFragment.getDialog().isShowing())) {
                    return true;
                }
            }
            return false;
        }

        public void ˊ() {
            if (this.ʾ != null) {
                DialogFragment dialogFragment = (DialogFragment) this.ʾ.get();
                if (dialogFragment != null && dialogFragment.getDialog() != null && dialogFragment.getDialog().isShowing()) {
                    dialogFragment.getDialog().dismiss();
                }
            }
        }
    }

    class b implements cks {
        final /* synthetic */ DialogActivity ˎ;

        private b(DialogActivity dialogActivity) {
            this.ˎ = dialogActivity;
        }

        public void enableNetworking() {
            if (this.ˎ.ˎ != null && this.ˎ.ˎ.ʽ != null && (this.ˎ.ˊ.getDialog() instanceof AlertDialog)) {
                this.ˎ.ˎ.ʽ.ˏ((AlertDialog) this.ˎ.ˊ.getDialog(), this.ˎ.ˎ, -1);
            }
        }

        public void upgrade() {
            if (this.ˎ.ˎ != null && this.ˎ.ˎ.ʽ != null && (this.ˎ.ˊ.getDialog() instanceof AlertDialog)) {
                this.ˎ.ˎ.ʽ.ˏ((AlertDialog) this.ˎ.ˊ.getDialog(), this.ˎ.ˎ, -2);
            }
        }

        public void ˋ() {
            if (this.ˎ.ˎ != null && this.ˎ.ˎ.ʽ != null && (this.ˎ.ˊ.getDialog() instanceof AlertDialog)) {
                this.ˎ.ˎ.ʽ.ˏ((AlertDialog) this.ˎ.ˊ.getDialog(), this.ˎ.ˎ, -3);
            }
        }
    }

    static class d extends Handler {
        private final WeakReference<DialogActivity> ˊ;

        public d(DialogActivity dialogActivity) {
            this.ˊ = new WeakReference(dialogActivity);
        }

        public void handleMessage(Message message) {
            DialogActivity dialogActivity = (DialogActivity) this.ˊ.get();
            if (dialogActivity != null && !dialogActivity.isFinishing() && message.what == DialogButton.CONFIRM && dialogActivity.ˎ != null && dialogActivity.ˊ != null) {
                dialogActivity.ˎ.ॱˋ = dialogActivity.ˎ.ॱˋ - 1;
                Dialog dialog;
                if (dialogActivity.ˎ.ॱˋ >= 0) {
                    dialog = dialogActivity.ˊ.getDialog();
                    if (dialog instanceof AlertDialog) {
                        ˎ(dialogActivity, (AlertDialog) dialog);
                    }
                    removeMessages(DialogButton.CONFIRM);
                    sendEmptyMessageDelayed(DialogButton.CONFIRM, 1000);
                    return;
                }
                dialog = dialogActivity.ˊ.getDialog();
                if (dialog instanceof AlertDialog) {
                    Button button = ((AlertDialog) dialog).getButton(dialogActivity.ˎ.ॱˎ);
                    if (button != null) {
                        button.performClick();
                    }
                }
            }
        }

        private void ˎ(DialogActivity dialogActivity, AlertDialog alertDialog) {
            Button button;
            if (-1 == dialogActivity.ˎ.ॱˎ) {
                button = alertDialog.getButton(-1);
                if (button != null) {
                    button.setText(dialogActivity.ˊ(dialogActivity.ˊ));
                }
            } else if (-2 == dialogActivity.ˎ.ॱˎ) {
                button = alertDialog.getButton(-2);
                if (button != null) {
                    button.setText(dialogActivity.ˎ(dialogActivity.ˊ));
                }
            } else if (-3 == dialogActivity.ˎ.ॱˎ) {
                button = alertDialog.getButton(-3);
                if (button != null) {
                    button.setText(dialogActivity.ॱ(dialogActivity.ˊ));
                }
            }
        }
    }

    protected void onCreate(Bundle bundle) {
        ayy.ˋ().ˎ(getWindow());
        requestWindowFeature(1);
        ayw.ॱ((Activity) this);
        super.onCreate(bundle);
        evd.ˏ(getWindow());
        Intent intent = getIntent();
        if (intent == null) {
            Logger.e("DialogActivity", "intent is null");
            finish();
            return;
        }
        this.ˋ = new SafeIntent(intent).getLongExtra("dialog_activity_task_id", 0);
        Object ˊ = bkv.ˎ().ˊ(this.ˋ);
        if (ˊ instanceof a) {
            this.ˎ = (a) ˊ;
        } else {
            this.ˎ = null;
        }
        if (this.ˎ == null) {
            Logger.e("DialogActivity", "can not find builder:" + this.ˋ);
            finish();
            return;
        }
        ˊ();
        if (this.ˎ.ˏॱ != null) {
            this.ˎ.ˏॱ.ॱ(this);
        }
        if (this.ˎ.ॱˋ > 0) {
            this.ॱ = new d(this);
            this.ॱ.sendEmptyMessage(DialogButton.CONFIRM);
        }
        Logger.ˎ("DialogActivity", "show dialog:" + this.ˎ.ˈ);
    }

    private void ˊ() {
        this.ˊ = cku.ˏ(this, cku.class, this.ˎ.ˏ, this.ˎ.ˎ);
        ˊ(this.ˊ);
        ˎ(this.ˊ);
        ॱ(this.ˊ);
        this.ˊ.ˎ(-1, this.ˎ.ॱ);
        this.ˊ.ˎ(-2, this.ˎ.ʼ);
        this.ˊ.ˎ(-3, this.ˎ.ᐝ);
        this.ˊ.ˎ(new OnDismissListener(this) {
            final /* synthetic */ DialogActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void onDismiss(DialogInterface dialogInterface) {
                if (this.ˏ.ˎ.ͺ != null) {
                    this.ˏ.ˎ.ͺ.onDismiss(dialogInterface);
                }
                this.ˏ.finish();
            }
        });
        this.ˊ.ॱ(new b());
        if (this.ˎ.ॱˊ != null) {
            this.ˊ.ˋ(this.ˎ.ॱˊ);
        }
        if (this.ˎ.ˋॱ != null) {
            this.ˊ.ˏ(this.ˎ.ˋॱ);
        }
        if (this.ˎ.ᐝॱ.size() > 0) {
            for (Entry entry : this.ˎ.ᐝॱ.entrySet()) {
                this.ˊ.ॱ(((Integer) entry.getKey()).intValue(), (c) entry.getValue());
            }
        }
        this.ˊ.setCancelable(this.ˎ.ॱᐝ);
        if (this.ˎ.ˊ != null) {
            this.ˊ.ˊ(this.ˎ.ˊ);
            if (this.ˎ.ˊॱ != null) {
                this.ˎ.ˊॱ.ॱ(this, this.ˎ.ˊ);
            }
        }
        this.ˊ.ˋ((Context) this, this.ˎ.ˈ);
        this.ˎ.ˏ(this.ˊ);
    }

    private CharSequence ˏ(CharSequence charSequence) {
        CharSequence spannableStringBuilder = new SpannableStringBuilder();
        if (!TextUtils.isEmpty(charSequence)) {
            spannableStringBuilder.append(charSequence);
        }
        spannableStringBuilder.append('（').append(String.valueOf(this.ˎ.ॱˋ)).append(')');
        return spannableStringBuilder;
    }

    private CharSequence ॱ(cku o_cku) {
        CharSequence ʻॱ;
        if (TextUtils.isEmpty(this.ˎ.ॱॱ) || this.ˎ.ॱˎ != -3) {
            ʻॱ = this.ˎ.ॱॱ;
        } else {
            ʻॱ = ˏ(this.ˎ.ॱॱ);
        }
        o_cku.ॱ(-3, this.ˎ.ॱॱ);
        return ʻॱ;
    }

    private CharSequence ˊ(cku o_cku) {
        CharSequence ᐝॱ;
        if (TextUtils.isEmpty(this.ˎ.ˋ) || this.ˎ.ॱˎ != -1) {
            ᐝॱ = this.ˎ.ˋ;
        } else {
            ᐝॱ = ˏ(this.ˎ.ˋ);
        }
        o_cku.ॱ(-1, this.ˎ.ˋ);
        return ᐝॱ;
    }

    private CharSequence ˎ(cku o_cku) {
        CharSequence ॱˋ;
        if (TextUtils.isEmpty(this.ˎ.ʻ) || this.ˎ.ॱˎ != -2) {
            ॱˋ = this.ˎ.ʻ;
        } else {
            ॱˋ = ˏ(this.ˎ.ʻ);
        }
        o_cku.ॱ(-2, this.ˎ.ʻ);
        return ॱˋ;
    }

    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        if (this.ˎ != null && this.ˎ.ˊॱ != null) {
            this.ˎ.ˊॱ.ॱ(this, this.ˎ.ˊ, configuration);
        }
    }

    protected void onDestroy() {
        if (this.ॱ != null) {
            this.ॱ.removeMessages(DialogButton.CONFIRM);
        }
        if (!(this.ˊ == null || this.ˊ.getDialog() == null)) {
            if (this.ˎ != null) {
                this.ˎ.ʻ();
            }
            this.ˊ.getDialog().dismiss();
        }
        if (!(this.ˎ == null || this.ˎ.ˏॱ == null)) {
            this.ˎ.ˏॱ.ˋ(this);
        }
        super.onDestroy();
        bkv.ˎ().ॱ(this.ˋ);
    }

    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
}
